package club.monkeywood.util.wechat;

import club.monkeywood.dto.wechat.message.*;
import club.monkeywood.enums.wechat.wxMessageEnum;
import club.monkeywood.dto.wechat.media.ImageMedia;
import com.thoughtworks.xstream.XStream;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;

public class MessageUtil{
//    将微信返回的xml转换为map
    public static Map<String,String> xmlToMap(HttpServletRequest request) throws IOException, DocumentException {
        Map<String,String>map=new HashMap<String,String>();
        SAXReader reader=new SAXReader();
        InputStream ins=request.getInputStream();
        Document doc=reader.read(ins);
        Element root=doc.getRootElement();
        List<Element> list=root.elements();
        for(Element e:list){
            map.put(e.getName(),e.getText());
        }
        ins.close();
        return map;
    }
    //将文本消息对象转为xml类型
    public static String textMessageToXml(TextMessage textMessage){
        XStream xstream=new XStream();

//        将根元素"全类名"替换为"xml"
        xstream.alias("xml", textMessage.getClass());

        return xstream.toXML(textMessage);
    }

    /**
     * 图文消息转为xml
     * @param newsMessage
     * @return
     */
    public static String newsMessageToXml(NewsMessage newsMessage){
        XStream xstream = new XStream();
        xstream.alias("xml", newsMessage.getClass());
        xstream.alias("item", NewsMessageItem.class);
        return xstream.toXML(newsMessage);
    }

    public static String imageMessageToXml(ImageMessage imageMessage){
        XStream xstream = new XStream();
        xstream.alias("xml", imageMessage.getClass());
        return xstream.toXML(imageMessage);
    }

    public static String musicMessageToXml(MusicMessage musicMessage){
        XStream xstream = new XStream();
        xstream.alias("xml", musicMessage.getClass());
        xstream.aliasAttribute( musicMessage.getClass(),"MusicMessageItem","Music" );
        return xstream.toXML(musicMessage);
    }



    //    初始化消息
    public static String initText(String toUserName,String fromUserName,String content){
        TextMessage text = new TextMessage();
        text.setFromUserName(toUserName);
        text.setToUserName(fromUserName);
        text.setMsgType(wxMessageEnum.MESSAGE_TEXT.getType());
        Long tm = new Date().getTime();
        text.setCreateTime(tm);
        text.setContent("您发送的消息是：" + content);
        return textMessageToXml(text);
    }

    public static String initImageMessage(String toUserName,String fromUserName){
        String message = null;
        ImageMedia imageMedia = new ImageMedia();
        imageMedia.setMedia_id("VZVtVZnTPgRn8zk962fgmDNC9cxa5z5Bkt7fA3lDLuJbTBbTlu6T15CNPSNxEtzk");
        ImageMessage imageMessage = new ImageMessage();
        imageMessage.setFromUserName(toUserName);
        imageMessage.setToUserName(fromUserName);
        imageMessage.setMsgType(wxMessageEnum.MESSAGE_IMAGE.getType());
        imageMessage.setCreateTime(new Date().getTime());
        imageMessage.setImageMedia(imageMedia);
        message = imageMessageToXml(imageMessage);
        return message;
    }

    /**
     * 图文消息的组装
     * @param toUserName
     * @param fromUserName
     * @return
     */
    public static String initNewsMessage(String toUserName,String fromUserName){
        String message = null;
        List<NewsMessageItem> newsMessageItemList = new ArrayList<NewsMessageItem>();
        NewsMessage newsMessage = new NewsMessage();

        NewsMessageItem newsMessageItem = new NewsMessageItem();
        newsMessageItem.setTitle("慕课网介绍1");
        newsMessageItem.setDescription("慕课网1是垂直的互联网IT技能免费学习网站。以独家视频教程、在线编程工具、学习计划、问答社区为核心特色。在这里，你可以找到最好的互联网技术牛人，也可以通过免费的在线公开视频课程学习国内领先的互联网IT技术。慕课网课程涵盖前端开发、PHP、Html5、Android、iOS、Swift等IT前沿技术语言，包括基础课程、实用案例、高级分享三大类型，适合不同阶段的学习人群。");
        newsMessageItem.setPicUrl("http://www.monkeywood.club/hm/api/image/monkey1.jpg");
        newsMessageItem.setUrl("www.imooc.com");
        newsMessageItemList.add(newsMessageItem);

        NewsMessageItem newsMessageItem2 = new NewsMessageItem();
        newsMessageItem2.setTitle("慕课网介绍2");
        newsMessageItem2.setDescription("慕课网2是垂直的互联网IT技能免费学习网站。以独家视频教程、在线编程工具、学习计划、问答社区为核心特色。在这里，你可以找到最好的互联网技术牛人，也可以通过免费的在线公开视频课程学习国内领先的互联网IT技术。慕课网课程涵盖前端开发、PHP、Html5、Android、iOS、Swift等IT前沿技术语言，包括基础课程、实用案例、高级分享三大类型，适合不同阶段的学习人群。");
        newsMessageItem2.setPicUrl("http://www.monkeywood.club/hm/api/image/monkey1.jpg");
        newsMessageItem2.setUrl("www.imooc.com");
        newsMessageItemList.add(newsMessageItem2);

        newsMessage.setToUserName(fromUserName);
        newsMessage.setFromUserName(toUserName);
        newsMessage.setCreateTime(new Date().getTime());
        newsMessage.setMsgType(wxMessageEnum.MESSAGE_NEWS.getType());
        newsMessage.setArticles(newsMessageItemList);
        newsMessage.setArticleCount(newsMessageItemList.size());

        message = newsMessageToXml(newsMessage);
        return message;
    }

    public static String initMusicMessage(String toUserName,String fromUserName) {
        String message = null;
        MusicMessageItem musicMessageItem = new MusicMessageItem();
        musicMessageItem.setThumbMediaId("BUz652VsA5qNUzP0NI9saDVUvWeDEnkEs7NE2dlvLu-rnxBs1-W2JMVjD1Dal4Gk");

        musicMessageItem.setTitle("see you again");
        musicMessageItem.setDescription("速7片尾曲");
        musicMessageItem.setMusicUrl("http://www.monkeywood.club/hm/api/music/SeeYouAgain.mp3");
        musicMessageItem.setHQMusicUrl("http://www.monkeywood.club/hm/api/music/SeeYouAgain.mp3");

        MusicMessage musicMessage = new MusicMessage();
        musicMessage.setFromUserName(toUserName);
        musicMessage.setToUserName(fromUserName);
        musicMessage.setMsgType(wxMessageEnum.MESSAGE_MUSIC.getType());
        musicMessage.setCreateTime(new Date().getTime());
        musicMessage.setMusicMessageItem(musicMessageItem);
        message = musicMessageToXml(musicMessage);
        return message;
    }

}